route.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/years", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-years-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. // Minimal structure for NL01
  19. await fs.mkdir(path.join(tmpRoot, "NL01", "2024"), { recursive: true });
  20. });
  21. afterEach(async () => {
  22. process.env.NAS_ROOT_PATH = originalNasRoot;
  23. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  24. });
  25. it("returns 401 when unauthenticated", async () => {
  26. getSession.mockResolvedValue(null);
  27. const res = await GET(
  28. new Request("http://localhost/api/branches/NL01/years"),
  29. {
  30. params: Promise.resolve({ branch: "NL01" }),
  31. }
  32. );
  33. expect(res.status).toBe(401);
  34. expect(await res.json()).toEqual({ error: "Unauthorized" });
  35. });
  36. it("returns 403 when branch user accesses a different branch", async () => {
  37. getSession.mockResolvedValue({
  38. role: "branch",
  39. branchId: "NL01",
  40. userId: "u1",
  41. });
  42. const res = await GET(
  43. new Request("http://localhost/api/branches/NL02/years"),
  44. {
  45. params: Promise.resolve({ branch: "NL02" }),
  46. }
  47. );
  48. expect(res.status).toBe(403);
  49. expect(await res.json()).toEqual({ error: "Forbidden" });
  50. });
  51. it("returns years for a valid branch when allowed", async () => {
  52. getSession.mockResolvedValue({
  53. role: "branch",
  54. branchId: "NL01",
  55. userId: "u1",
  56. });
  57. const res = await GET(
  58. new Request("http://localhost/api/branches/NL01/years"),
  59. {
  60. params: Promise.resolve({ branch: "NL01" }),
  61. }
  62. );
  63. expect(res.status).toBe(200);
  64. const body = await res.json();
  65. expect(body).toEqual({ branch: "NL01", years: ["2024"] });
  66. });
  67. it("returns 400 when branch param is missing (authenticated)", async () => {
  68. getSession.mockResolvedValue({
  69. role: "admin",
  70. branchId: null,
  71. userId: "u2",
  72. });
  73. const res = await GET(new Request("http://localhost/api/branches//years"), {
  74. params: Promise.resolve({ branch: undefined }),
  75. });
  76. expect(res.status).toBe(400);
  77. expect(await res.json()).toEqual({ error: "branch Parameter fehlt" });
  78. });
  79. it("returns 500 when NAS_ROOT_PATH is invalid (authenticated)", async () => {
  80. getSession.mockResolvedValue({
  81. role: "admin",
  82. branchId: null,
  83. userId: "u2",
  84. });
  85. process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
  86. const res = await GET(
  87. new Request("http://localhost/api/branches/NL01/years"),
  88. {
  89. params: Promise.resolve({ branch: "NL01" }),
  90. }
  91. );
  92. expect(res.status).toBe(500);
  93. const body = await res.json();
  94. expect(body.error).toContain("Fehler beim Lesen der Jahre:");
  95. });
  96. });